"use strict";

module.exports = (app) => {
  class User extends app.Controller {
    async register() {
      const { ctx } = this;
      const { username, password } = ctx.request.body;
      // 判断是否为空
      if (!username || !password) {
        // 如果为空就返回账号或者密码不能为空
        ctx.body = {
          code: 500,
          msg: "账号或者密码不能为空",
          data: null,
        };
        return;
      }

      // 判断是否注册过
      const userinfo = await ctx.service.user.getUserByName(username);

      if (userinfo && userinfo.id) {
        // 已经注册过
        ctx.body = {
          code: 500,
          msg: "账号已经注册过",
          data: null,
        };
        return;
      }

      // 注册
      const avatar =
        "http://s.yezgea02.com/1615973940679/WeChat77d6d2ac093e247c361f0b8a7aeb6c2a.png";
      const result = await ctx.service.user.register({
        username,
        password,
        avatar,
        signature: "默认签名",
      });
      if (result) {
        ctx.body = {
          code: 200,
          msg: "注册成功",
          data: null,
        };
      } else {
        ctx.body = {
          code: 500,
          msg: "注册失败",
          data: null,
        };
      }
    }
    async login() {
      const { ctx } = this;
      const { username, password } = ctx.request.body;
      // 判断账号或密码是否为空
      if (!username || !password) {
        ctx.body = {
          code: 500,
          msg: "账号或者密码不能为空",
          data: null,
        };
        return;
      }
      // 判断账号是否存在
      const userinfo = await ctx.service.user.getUserByName(username);
      if (!userinfo || !userinfo.id) {
        ctx.body = {
          code: 500,
          msg: "账户不存在",
          data: null,
        };
        return;
      }
      // 判断密码是否错误
      if (userinfo && userinfo.password !== password) {
        ctx.body = {
          code: 500,
          msg: "密码错误",
          data: null,
        };
        return;
      }

      // 登录账号
      const token = app.jwt.sign(
        {
          id: userinfo.id,
          username: userinfo.username,
          exp: Math.floor(Date.now() / 1000) + 24 * 60 * 60,
        },
        app.config.secret
      );

      ctx.body = {
        code: 200,
        msg: "登录成功",
        data: {
          token,
        },
      };
    }
    async test() {
      const { ctx, app } = this;
      const token = ctx.request.header.authorization;
      console.log(token);
      const decode = app.jwt.verify(token, app.config.jwt.secret);
      ctx.body = {
        code: 200,
        msg: "获取成功",
        data: {
          ...decode,
        },
      };
    }
    async getUserInfo() {
      //获取用户信息
      const { ctx, app } = this;
      const token = ctx.request.header.authorization;
      const decode = await app.jwt.verify(token, app.config.jwt.secret);
      const userInfo = await ctx.service.user.getUserByName(decode.username);
      ctx.body = {
        code: 200,
        msg: "操作成功",
        data: {
          userInfo,
        },
      };
    }
    async editUserInfo() {
      //更新用户信息
      const { ctx, app } = this;
      const { signature } = ctx.request.body;

      const token = ctx.request.header.authorization;
      const decode = await app.jwt.verify(token, app.config.jwt.secret);
      if (!decode) {
        return;
      }
      try {
        let userInfo = await ctx.service.user.getUserByName(decode.username); //获取用户信息
        // console.log(userInfo);
        userInfo["signature"] = signature;
        // console.log(userInfo);
        let result = await ctx.service.user.editUserInfo(
          //修改用户信息
          userInfo
        );
        if (result) {
          ctx.body = {
            code: 200,
            message: "修改成功",
            data: null,
          };
        }
      } catch (error) {
        console.log(error);
      }
    }
  }
  return User;
};
